from random import shuffle

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

###############################################################
# generate_substitution_key()
# A utility function for generating random permutations of 
# the alphabet.
def generate_substitution_key():
    # Randomly shuffle the alphabet
	randarray=range(0,len(alphabet))
	shuffle(randarray)
	key=""
	for i in range(0,len(alphabet)):
		key+=alphabet[randarray[i]]
	return key


###############################################################
# The key for a substitution cypher is a permutation on the 
# standard alphabet.
def substitution_encrypt(plainText, key):
	# Convert each letter of plaintext to the corrsponding
	# encrypted letter in our key
    plainText = plainText.upper()
    cipherText = ""
    for c in range(len(plainText)):
        if plainText[c] in alphabet: 
            cipherText += key[alphabet.find(plainText[c])] 
        else:
            cipherText += plainText[c]
    return cipherText


###############################################################
def substitution_decrypt(cipherText, key):
    plainText = ""
    for c in range(len(cipherText)):
        if cipherText[c] in key:
            plainText += alphabet[key.find(cipherText[c])]
        else:
            plainText += cipherText[c]
    return plainText



